<?php
/**
 * 加密解密 字符串
 * @param string $string 原字符串
 * @param string  $auth_key 加密的KEY
 * @param string $operation  加密解密的开关   ENCODE：加密， DECODE：解密
 * @return string|mixed
 */
function authCode($string, $auth_key, $operation='ENCODE') {
	$key = md5($auth_key);
	$key_length = strlen($key);

	//base64_decode 默认是 iso-8859-1 转 UTF-8 用 utf8_decode(base64_decode($string))
	$string = $operation == 'DECODE' ? utf8_decode(base64_decode($string)) : substr(md5($string.$key), 0, 16).$string;
	$string_length = strlen($string);

	$rndkey = $box = array();
	$result = '';

	for($i = 0; $i <= 255; $i++) {
		$rndkey[$i] = ord($key[$i % $key_length]);
		$box[$i] = $i;
	} 
 
	for($j = $i = 0; $i < 256; $i++) {
		$j = ($j + $box[$i] + $rndkey[$i]) % 256;
		$tmp = $box[$i];
		$box[$i] = $box[$j];
		$box[$j] = $tmp;
	}

	for($a = $j = $i = 0; $i < $string_length; $i++) {
		$a = ($a + 1) % 256;
		$j = ($j + $box[$a]) % 256;
		$tmp = $box[$a];
		$box[$a] = $box[$j];
		$box[$j] = $tmp;
		$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
	}

	if($operation == 'DECODE') {
		if(substr($result, 0, 16) == substr(md5(substr($result, 16).$key), 0, 16)) {
			return substr($result, 16);
		} else {
			return '';
		}
	} else {
		//base64_encode 默认是 iso-8859-1 转 UTF-8 用 base64_encode(utf8_encode($result))
		return str_replace('=', '',base64_encode(utf8_encode($result)));
	}
}

$key = $_GET['key'];
$string = $_GET['string'];
$a = authCode($string, $key);
echo $a;
//var_dump($a);
//var_dump(authCode($a, $key,'DECODE'));
?>